package com.dating.utils;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.nutz.lang.Times;

public class DateUtil extends Times {
	public static Date addDays(Date base, int days) {
		return D(base.getTime() + days * 24 * 60 * 60 * 1000L);
	}

	public static Date addDays(int days) {
		return addDays(now(), days);
	}

	public static double daysBetween(Date start, Date end) {
		double i = (end.getTime() - start.getTime()) / 1000L / 24L / 60L
				/ 60.0D;
		return new BigDecimal(i).setScale(2, 6).doubleValue();
	}

	public static Date getDayStart(Date d) {
		return D(format("yyyy-MM-dd 00:00:00", d));
	}

	public static Date getDayEnd(Date d) {
		return D(format("yyyy-MM-dd 23:59:59", d));
	}

	public static Date getDayStart() {
		return D(format("yyyy-MM-dd 00:00:00", now()));
	}

	public static Date getDayEnd() {
		return D(format("yyyy-MM-dd 23:59:59", now()));
	}

	public static Date getWeekEnd(boolean startFromSunday) {
		Date date = addDays(getWeekStart(startFromSunday), 6);
		return getDayEnd(date);
	}

	public static Date getWeekEnd(boolean startFromSunday, Date date) {
		Date newDate = addDays(getWeekStart(startFromSunday, date), 6);
		return getDayEnd(newDate);
	}

	public static Date getWeekStart(boolean startFromSunday) {
		return addDays(getDayStart(), -1
				* (Times.C(now()).get(7) - 1 - ((startFromSunday) ? 0 : 1)));
	}

	public static Date getWeekStart(boolean startFromSunday, Date date) {
		return addDays(getDayStart(date), -1
				* (Times.C(now()).get(7) - 1 - ((startFromSunday) ? 0 : 1)));
	}

	public static Date addSeconds(Date base, long seconds) {
		return D(base.getTime() + seconds * 1000L);
	}

	public static Date addSeconds(long seconds) {
		return addSeconds(now(), seconds);
	}

	public static Date getYearStart(Date d) {
		String year = format("yyyy", d);
		year = year + "-01-01";
		return D(year);
	}

	public static Date getYearStart() {
		return getYearStart(now());
	}

	public static Date getYearEnd(Date d) {
		String year = format("yyyy", d);
		year = year + "-12-31";
		return getDayEnd(D(year));
	}

	public static Date getYearEnd() {
		return getYearEnd(now());
	}

	public static Date getMonthStart(Date d) {
		String month = format("yyyy-MM", d) + "-01";
		return D(month);
	}

	public static Date getMonthStart() {
		return getMonthStart(now());
	}

	public static Date getMonthEnd(Date d) {
		Calendar ca = Calendar.getInstance();
		ca.setTime(d);
		ca.set(5, ca.getActualMaximum(5));
		return getDayEnd(ca.getTime());
	}

	public static void main(String[] args) {
		System.err.println(getWeekEnd(false));
	}

	public static Date getMonthEnd() {
		return getMonthEnd(now());
	}

	public static String getSequence() {
		String sequence = format("yyyyMMDD", now());
		int _x = ms();
		sequence = sequence + (_x % 10000);
		return sequence;
	}

	public static String getYearStartMonth() {
		return format("yyyy-MM", getYearStart());
	}

	public static String getYearStartMonth(Date date) {
		return format("yyyy-MM", getYearStart(date));
	}

	public static String getYearEndMonth() {
		return format("yyyy-MM", getYearEnd());
	}

	public static String getYearEndMonth(Date date) {
		return format("yyyy-MM", getYearEnd(date));
	}

	public static String getMonthStartDate() {
		return format("yyyy-MM-dd", getMonthStart());
	}

	public static String getMonthStartDate(Date date) {
		return format("yyyy-MM-dd", getMonthStart(date));
	}

	public static String getMonthEndDate() {
		return format("yyyy-MM-dd", getMonthEnd());
	}

	public static String getMonthEndDate(Date date) {
		return format("yyyy-MM-dd", getMonthEnd(date));
	}

	public static int getDayOfMonth() {
		Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
		int day = aCalendar.getActualMaximum(5);
		return day;
	}
}
